﻿<?php
require_once '../models/Product.php';
require_once '../models/Customer.php';

$total=0;
if(isset($_SESSION['cart'])){
$check=true;
if(isset($_POST['btn_submit']))
{
	
	$sl_update=$_POST['soluong'];
	foreach($sl_update as $key=>$value){
				
		if($value==0){
			unset($_SESSION['cart'][$key]);
		}
		else if($value>0&&is_numeric($value)){
			$_SESSION['cart'][$key]=$value;
		}
		$get=Product::getInfoProduct($key);
				if($get['quantity']<$value){
				echo "<script>alert('Sản phẩm trong kho không đủ');window.location='cart.php'</script>";
				$_SESSION['cart'][$key]=1;
				$check=false;
				}
		//header('location: cart.php');
	}
}

echo "<form method='POST'><h1>Giỏ hàng";
echo "</h1>";
			print('<table id="table_cart_list">
			<thread>
			<tr class="table_head">
				<td colspan="2"><h4>Sản phẩm</h4></td>
				<td><h4>Số lượng</h4></td>
				<td><h4>Đơn giá</h4></td>
				<td><h4>Thành tiền</h4></td>
				<td><h4>Xóa</h4></td>
			</tr>
			</thread><tbody>
			');
foreach($_SESSION['cart'] as $key=>$value)
{
			$query="SELECT * FROM tbl_product WHERE productID IN ('$key')";
			$result=mysql_query($query) or die("cart_list:".mysql_error());

			while($row=mysql_fetch_array($result)){
				$row=Product::getInfoProduct($row['productID']);
				print('<tr>
							<td ><a href="productDetail.php?productID='.$row['productID'].'"><img src='.$row['image'].' class="img_w144_h130"></img></td>

							<td>'.$row['brandName'].' '.$row['modelName'].' '.$row['seriesName'].'</td>
							<td> <input type="number" onchange="check_quantity();" class="number" value="'.$value.'" name="soluong['.$key.']"/></td>
							<td>'.number_format($row['price'],3).'</td>
							<td>'.number_format($row['price']*$value,3).'</td>
							<td><a href="del_cart.php?productID='.$row['productID'].'"><img src="../images/del.png" class="icon"></a></td>
							</tr>
					');
						$total+=$value*$row['price'];
					$_SESSION["total"]=$total;	
				
						
			}
}
print('</tbody></table>');

echo "<div class='list-total'><div><span class='font_20_cl_red'>Tổng:". number_format($total,3)." VND</span></div></div>";
echo "";
}else{
echo "<script>alter(Gio hang cua ban khong co j);window.location='home.php';</script>";
}
	if(isset($_SESSION['cart'])>0){
		echo "<div id='float_left'><a href='index.php'><input type='button' class='btn_button_' value='Mua thêm'></a></div>";
		echo "<div id='float_right'><a href='address.php'><input type='button' class='btn_button_' value='Bước tiếp theo'></a></div>";
		echo	"<div id='float_right'><input type='submit' name='btn_submit' id='submit' class='btn_button_' value='Cập nhật' ></div>";	
	}
?>
</form>